<?php

/* 样板 order */
class OrderModel extends BaseModel {
    var $table  = 'order';
    var $prikey = 'order_id';
    var $_name  = 'o';
    var $alias  = 'o';
    var $_common_data=array();
    var $_relation = array(
            'has_sourcing' => array(	//生产板采购单关联关系
                 'model' => 'sourcing',
                 'type' => HAS_MANY,
                 'foreign_key'   => 'order_id',
                 'dependent'     => true
            ),
    		'has_bulk' => array(	//大货采购单关联关系
    				'model' => 'bulk_sourcing',
    				'type' => HAS_ONE,
    				'foreign_key'   => 'item_id',
    				'dependent'     => true
    		),
    		'has_process' => array(	//qc查货进度表关联关系
    				'model' => 'qc_process',
    				'type' => HAS_MANY,
    				'foreign_key'   => 'order_id',
    				'dependent'     => true
    		),
            'belongs_brand' => array(
                 'model' => 'brand',
                 'type' => BELONGS_TO,
                 'foreign_key' => 'brand_id',
                 'reverse'  => 'has_order',
            ),
            'belongs_buyer' => array(
                 'model' => 'buyer',
                 'type' => BELONGS_TO,
                 'foreign_key' => 'buyer_id',
                 'reverse'       => 'has_order',
            ),
            'belongs_merchandiser' => array(
                 'model' => 'member',
                 'type' => BELONGS_TO,
                 'foreign_key' => 'merchandiser_id',
                 'reverse'       => 'has_order',
            ),
            'belongs_style' => array(
                 'model' => 'style',
                 'type' => BELONGS_TO,
                 'foreign_key' => 'style_id',
                 'reverse'       => 'has_order',
            ),
            'belongs_special' => array(
                 'model' => 'special',
                 'type' => BELONGS_TO,
                 'foreign_key' => 'special_id',
                 'reverse'       => 'has_order',
            ),
            'belongs_factory' => array(
                 'model' => 'factory',
                 'type' => BELONGS_TO,
                 'foreign_key' => 'factory_id',
                 'reverse'       => 'has_order',
            ),
			'has_ss' => array(
                 'model' => 'ss',
                 'type' => HAS_ONE,
                 'foreign_key'   => 'order_id',
                 'reverse'       => 'belongs_order',
				 'dependent' => true
            ),
            'has_chuhuo' => array(
	            'model'         => 'chuhuo',
	            'type'          => HAS_ONE,
	            'foreign_key'   => 'order_id',
	            'dependent' => true
             ),
			 'has_salesre' => array(
	            'model'         => 'order_sales_return',
	            'type'          => HAS_MANY,
	            'foreign_key'   => 'order_id',
	            'dependent'     => true
	       	    ),
	    	'has_quote' => array(
	    		'model'         => 'quote',
	    		'type'          => HAS_MANY,
	    		'foreign_key'   => 'order_id',
	    		'dependent'     => true
	    	),
    		'has_receipt' => array(
    				'model'         => 'receipt',
    				'type'          => HAS_MANY,
    				'foreign_key'   => 'order_id',
    				'dependent'     => true
    		),
    );
    var $_autov = array(
            'order_create_date'=>array(
                            'filter'    => '_format_time',
            ),
            'arrival_date' => array(
                            'filter'    => '_format_time',
            ),
            'buyer_arrival_date' => array(
                            'filter'    => '_format_time',
            ),
            'release_date' => array(
                            'filter'    => '_format_time',
            ),
            'sign_contract_date' => array(
                            'filter'    => '_format_time',
            ),
            'send_bulk_date' => array(
                            'filter'    => '_format_time',
            ),
            'reply_bulk_date' => array(
                            'filter'    => '_format_time',
            ),
            'middle_check_date' => array(
                            'filter'    => '_format_time',
            ),
            'last_check_date' => array(
                            'filter'    => '_format_time',
            ),
            'bulk_shipment_date' => array(
                            'filter'    => '_format_time',
            ),

            'before_sample_date' => array(
                            'filter'    => '_format_time',
            ),
            'confirm_date' => array(
                            'filter'    => '_format_time',
            ),
            'before_sample_reply_date'=> array(
                            'filter'    => '_format_time'
            )
    );
	
	/*function find($params)
	{
		$return = parent::find($params);
		$file_mod =& m('upload');
		foreach ($return as $key => $val) {
			if ($val['sample_id']) {
				$return[$key]['factory_files'] = $file_mod->find(array(
					'conditions' => 'key_id = ' . $val['sample_id'] . ' AND file_type = ' . BELONGS_FACTORY,
					'order' => 'file_id ASC'
				));
			}
		}
		return $return;
	}*/

    function get_common_data($key) {
        //echo 'get :'.$key .'<br />';
        if(isset($this->_common_data[$key])) {
            return $this->_common_data[$key];
        }
        else {
            $data=$this->_get_common_data($key);
            $this->set_common_data($key,$data);
            return $data;
        }
    }
    function _get_common_data($key) {
        $model=&m($key);
        $filter=array();
        if(in_array($key, array('style','special','member','brand','factory','fabric'))) {
            if ($key=='factory'){
            	$filter['conditions']='if_show=1 and activation=1';
            }else{
            	$filter['conditions']='if_show=1';
            }
        }
		if($key=="brand")
		{
			$filter['join']="belong_company";
		}
		if($key=="member")
		{
			$filter['conditions']="activation=1 and role=".USER_CLIENT;
		}
		if (in_array($key, array('brand', 'fabric_shop'))) {
			$filter['order'] = $key . '_name ASC';
		}
        if($key=='sample'){
            $filter['conditions'].=' status!=99';
			$filter['order'] = 'sample_sn ASC';
        }
        //echo 'get :'.$key .'<br />';
        $data=$model->find($filter);
        //print_r($data);
        return $data;
    }
    function set_common_data($key,$data) {
        $this->_common_data[$key]=$data;
    }

    function _format_order($order) {
        if(isset($order['style_id'])) {
            $style=$this->get_common_data('style');
            $order['style_name']=$style[$order['style_id']]['style_name'];
        }
        if(isset($order['sample_id'])) {
            $sample=$this->get_common_data('sample');
            $order['sample_sn']=$sample[$order['sample_id']]['sample_sn'];
        }
        if(isset($order['special_id'])) {
            $special=$this->get_common_data('special');
            $order['special_name']=$special[$order['special_id']]['special_name'];
        }
        if(isset($order['buyer_id'])) {
            $buyer=$this->get_common_data('member');
            $order['buyer_name']=$buyer[$order['buyer_id']]['user_name'];
        }
        if(isset($order['brand_id'])) {
            $brand=$this->get_common_data('brand');
            $order['brand_name']=$brand[$order['brand_id']]['brand_name'];
        }
        if(isset($order['brand_addr'])) {
        	$order['brand_addr']=$order['brand_addr'];
        }
        if(isset($order['factory_id'])) {
            $factory=$this->get_common_data('factory_info');
            $order['factory_name']=$factory[$order['factory_id']]['factory_name'];
        }
        if(isset($order['merchandiser_id'])) {
            $member = &m('member')->get($order['merchandiser_id']);
            $order['merchandiser']=$member['user_name'];
        }
        return $order;
    }
}
function _format_time($time) {
    if(!$time) {
        return '';
    }
    elseif(is_numeric($time)) {
        return $time;
    }
    else {
        return strtotime($time);
    }
}
?>